package com.shkco.controller;

import com.shkco.dto.StockDto;
import com.shkco.service.IStockService;
import com.shkco.domain.Stock;
import com.shkco.xbjcc.query.StockQuery;
import com.shkco.result.JSONResult;
import com.shkco.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/stock")
public class StockController {

    @Autowired
    public IStockService stockService;


    @RequestMapping(value="/inBound",method= RequestMethod.POST)
    public JSONResult inBound(@RequestBody @Valid StockDto stockDto){
        stockService.inBound(stockDto);
        return JSONResult.success();
    }

    @RequestMapping(value="/through/{id}",method= RequestMethod.POST)
    public JSONResult through(@PathVariable("id")Long id){
        stockService.outBound(id);
        return JSONResult.success();
    }

    @RequestMapping(value="/notThrough/{id}",method= RequestMethod.POST)
    public JSONResult notThrough(@PathVariable("id")Long id){
        stockService.noOutBound(id);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Stock stock){
        if(stock.getId()!=null){
            stockService.updateById(stock);
        }else{
            stockService.insert(stock);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        stockService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(stockService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(stockService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody StockQuery query){
        Page<Stock> page = new Page<Stock>(query.getPage(),query.getRows());
        page = stockService.selectPage(page);
        return JSONResult.success(new PageList<Stock>(page.getTotal(),page.getRecords()));
    }
}
